package cn.appsys.controller.backend;

import cn.appsys.pojo.BackendUser;
import cn.appsys.pojo.DevUser;
import cn.appsys.service.developer.DevUserService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
import cn.appsys.tools.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/manager/backend/user")
public class UserManagementController {
    @Autowired
    private DevUserService devUserService;
    @RequestMapping("/userlist")
    public String toUserManagement(String devName, String pageIndex, Model model) {
        System.out.println("============================toUserManagement============================");
        //这是pagesize
        int pageSize = Constants.pageSize;
        //设置当前页码
        Integer currentPageNo = 1;
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("beginNo", (currentPageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        map.put("devName", devName);
        //总记录数
        int totalCount = 0;
        try {
            totalCount = devUserService.queryDevUserCountByCondition(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //校验页码
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        List<DevUser> devUserList = null;
        try {
            devUserList = devUserService.queryDevUserByConditionForPage(map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute("devUserList", devUserList);
        model.addAttribute("pages", pages);
        model.addAttribute("devName", devName);
        return "backend/usermanagement";
    }

    @RequestMapping("/deleteDevuser")
    @ResponseBody
    public Object deleteDevUserById(int id){
        ReturnObject returnObject = new ReturnObject();
        try {
            int ret = devUserService.deleteDevUserById(id);
            if (ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("删除失败，请稍后重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败，请稍后重试！");
        }
        return returnObject;
    }

    @RequestMapping("/toadduser")
    public String toAddUser(){
        return "backend/adduser";
    }

    @RequestMapping("/saveDevUser")
    @ResponseBody
    public Object saveDevUser(DevUser devUser, HttpSession session){
        Date date = new Date();
        ReturnObject returnObject = new ReturnObject();
        BackendUser loginBackendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
        System.out.println(loginBackendUser);
        //封装参数
        devUser.setCreatedBy(loginBackendUser.getId());
        devUser.setCreationDate(date);
        devUser.setState(0);

        try {
            int ret = devUserService.savaCreateDevUser(devUser);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("添加失败，请稍后重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加失败，请稍后重试！");
        }
        return returnObject;
    }

    @RequestMapping("/toedituser")
    public String toEditUser(int id,Model model){
        DevUser devUser = null;
        try {
            devUser = devUserService.queryDevUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("devUser",devUser);
        return "backend/edituser";
    }

    @RequestMapping("/editUser")
    @ResponseBody
    public Object editUser(DevUser devUser,HttpSession session){
        BackendUser loginBackendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
        ReturnObject returnObject = new ReturnObject();
        devUser.setModifyBy(loginBackendUser.getId());
        devUser.setModifyDate(new Date());
        try {
            int ret = devUserService.editDevUserById(devUser);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("修改失败，请稍后再试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("修改失败，请稍后再试！");
        }
        return returnObject;
    }
}
